var TaskList = require('../model/taskList').TaskList
const axios = require('axios')
const md5 = require('md5')
const User = require('../model/user').User
const Company = require('../model/company')
const sequelize = require('../config/index')
var validator = require('validator');
const Sequelize = require('sequelize')
let request = require('request')
const Op = Sequelize.Op;
const result = require('../utils/result')
/**
 * 身份切换
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
exports.switch = async (req,res)=>{
    function getInfo(_json) {
        Company.findOne({
            where:{
                uid:req.query.uid
            }
        }).then(_result=>{
            if(!_result){
                User.findAll({
                    where:{
                        uid:req.query.uid
                    }
                }).then(_result2=>{
                    console.log(_result2[0].uid)
                    Company.create({
                        uid:_result2[0].uid,
                        avatar: _result2[0].headimgurl
                    }).then(v=>{
                        res.json(result(0,'success',_json))
                    })
                })
            }else{
                res.json(result(0,'success',_json))
            }
        }).catch(err=>{
            // console.log(err)
        })
    }
    if(!req.cookies.identity){ /*company*/
        res.cookie('identity','company')
        getInfo({identity:'company'})
    }else{
        if(req.cookies.identity =='default' ){
            res.cookie('identity','company')
            getInfo({identity:'company'})

        }else if(req.cookies.identity =='company' ){
            res.cookie('identity','default')
            getInfo({identity:'default'})
        }else{
            res.json(result(0,'success'))
        }
    }
}
/**
 * 创建任务
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
exports.createTask=async (req,res)=>{

    let sour = req.cookies.identity =="company"?'企业':'个人'
    let user = await User.findOne({where:{uid:req.query.uid}});
    let name = ''
    if(user.name&&user.name !==null){
        name = user.name
    }else{
        name = user.nickname;
    }
    TaskList.create(
        {
            headimgurl:user.headimgurl,
            sour:sour,
            uname:name,
            name:req.query.name,
            time:req.query.time,
            cut_time:req.query.cut_time,
            uid:req.query.uid,
            describe:req.query.describe,
            price:req.query.price,
            showprice:Number(req.query.price)*(0.85),
            links1:req.query.links1,
            img_url1:req.query.img_url1,
            img_url2:req.query.img_url2,
            address:req.query.address,
            type:req.query.type}).then( v =>{
                User.findOne({where:{uid:req.query.uid}}).then(r=>{
                    axios.get('http://localhost:3100/wx/message',{params:{
                            select:'task_create_success',
                            openid:r.openid,
                            first:"任务发布成功",
                            name:req.query.name,
                            status:"任务创建成功,等待审核"
                        }}).then(rs=>{
                        res.json(result(0,'success'))
                    })
                })

    }).catch(err=>{
        res.json(result(-1,'发生错误'))
    })
}

/**
 * 显示所有任务
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
exports.showTaskList=async (req,res)=>{
    let source = req.query.source
    if(source =='admin'){
        User.findAll().then(v=>{
            TaskList.findAll().then(v2=>{
                let arry = []
                arry = arry.concat(v2)
                res.json(result(0,'success',v2))
            })
        })
    }else{
        User.findAll().then(v=>{
            TaskList.findAll({ order: sequelize.literal('id DESC')},{
                where: {
                    isDel: 0,
                    status: {
                        [Op.in]: [0, 1]
                    },
                }
            }).then(v2=>{
                res.json(result(0,'success',v2))
            })
        })
    }

}
/**
 * 线上兼职
 * @param req
 * @param res
 * @returns {Promise<void>}
 * @constructor
 */
exports.OnlineTaskList = async (req,res) =>{
    let _data = await TaskList.findAll({
        where: {
            isDel: 0,
            status: 1,
            type:'线下兼职'
        }
    },{order : 'id asc'})
    if(_data&&_data.length>0){
        res.json(result(0,'success',_data))
    }else{
        res.json(result(-1,'fail','赞无数据'))
    }
}
/**
 * 技能兼职
 * @param req
 * @param res
 * @returns {Promise<void>}
 * @constructor
 */
exports.SkillTaskList = async (req,res) =>{
    let _data = await TaskList.findAll({
        where: {
            isDel: 0,
            status: 1,
            type:'技能兼职'
        }
    },{order : 'id asc'})
    if(_data&&_data.length>0){
        res.json(result(0,'success',_data))
    }else{
        res.json(result(-1,'fail','赞无数据'))
    }
}
/**
 * 线下兼职
 * @param req
 * @param res
 * @returns {Promise<void>}
 * @constructor
 */
exports.offlineTaskList = async (req,res) =>{
    let _data = await TaskList.findAll({
        where: {
            isDel: 0,
            status: 1,
            type:'线下兼职'
        }
    },{order : 'id asc'})
    if(_data&&_data.length>0){
        res.json(result(0,'success',_data))
    }else{
        res.json(result(-1,'fail','赞无数据'))
    }
}
/**
 * 任务审核
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
exports.auditing_task = async (req,res)=>{
    let taskid = req.query.taskid ;
    if(!taskid){res.json(result(-1,'fail','需要传taskid'));return}
    let Result = await TaskList.findAll({
        where:{
            id:taskid,
            status:0
        }
    })
    let rs = await TaskList.findOne({where:{id:taskid}})
    if(Result&&Result.length>0){
        TaskList.update({status:1},
            {where:{
                    id:taskid,
                    status:0
                }}).then( v=>{
            if(v&& v.length>0 && v[0]!==0){
                // res.json(result(0,'success','更改成功'))
                User.findOne({where:{uid:req.query.uid}}).then(r=>{
                    axios.get('http://localhost:3100/wx/message',{params:{
                            select:'task_create_success',
                            first:"您提交的兼职信息已经审核完毕",
                            openid:r.openid,
                            name:rs.name,
                            type:'任务审核结果通知',
                            user:r.nickname,
                            content:"您发布的任务已功通过审核"
                        }}).then(rs=>{
                        res.json(result(0,'success','状态改为审核通过'))

                    })
                })
            }else{
                res.json(result(-1,'fail','更改失败'))
            }
        }).catch(err=>{
            res.json(result(-1,'fail','找不到该任务'))
        });

    }else{
        res.json(result(-1,'','出错了'))

    }
}

/**
 * 任务删除
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
exports.delete_task = async (req,res)=>{
    let taskid = req.query.taskid ;
    if(!taskid){res.json(result(-1,'fail','需要传taskid'));return}
    let Result = await TaskList.findAll({
        where:{
            id:taskid
        }
    })
    if(Result&&Result.length>0){
        TaskList.update({isDel:1},
            {where:{
                    id:taskid,
                    isDel:0
                }}).then( v=>{
            if(v&& v.length>0 && v[0]!==0){
                res.json(result(0,'success','删除成功'))
            }else{
                res.json(result(-1,'fail','删除失败'))
            }
        }).catch(err=>{
            res.json(result(-1,'fail','删除失败'))
        });

    }else{
        res.json(result(-1,'','删除失败'))

    }
}

/**
 * 任务详情
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
exports.detail_task = async (req,res)=>{
    let uid = req.query.uid
    let type = req.query.type || '个人';
    let task = ''
    let taskid = req.query.taskid;
    let _g = ''
    if(type == '个人'){
       _g = await User.findOne({where:{uid:uid}})
    }else{
        _g = await Company.findOne({where:{uid:uid}})
    }
    task = await TaskList.findOne({where:{id:taskid}})
    let company = _g
    res.json(result(0,'success',{task,company}))

}


/**
 * 历史任务
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
exports.history_task = async (req,res)=>{
    let uid = req.query.uid
    let G = await TaskList.findAll({where:{uid:uid,isDel:0,status:{[Op.gt]: 0, }}})
    res.json(result(0,'success',G))

}
